package com.marvels.approve.process.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.marvels.approve.common.dto.EstateInfoDTO;
import com.marvels.approve.common.dto.RiskCheckInfoDTO;
import com.marvels.approve.common.dto.TrickCheckInfoDTO;
import com.marvels.approve.loan.dto.McamLoanApplyInfoDTO;
import com.marvels.approve.manager.dto.OpinionTypeDTO;
import com.marvels.approve.process.dto.LoanApplyCaseDTO;
import com.marvels.approve.process.dto.SysUserDTO;
import com.marvels.approve.process.service.TaskProcessService;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;

/**
 * 审批管理-任务处理
 * @author wangliang
 *
 */
@RestController
@RequestMapping("/approve/process/task_process/service")
public class TaskProcessingServiceController {
	@Autowired
	private TaskProcessService taskProcessService;
	
	/**
	 * 获取进件子系统基表数据-贷款信息tab
	 * @return
	 */
	@RequestMapping("/getBasicDatas")
	@ResponseBody
	public MarvelsResultDTO getProducerBasicDatas(){
		MarvelsLogUtil.getInstance().error("test sleuth : system call approve");
		MarvelsResultDTO result = null;
		try {
			result = taskProcessService.getBasicDatas();
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getProducerBasicDatas] error: ",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return result;
	}
	
	/**
	 * 获取进件子系统基表数据-贷款信息tab
	 * @return
	 */
	@RequestMapping("/getUploadAttamentTypes")
	@ResponseBody
	public MarvelsResultDTO getUploadAttamentTypes(@RequestBody String loanId){
		MarvelsResultDTO result = null;
		try {
			result = taskProcessService.getUploadAttamentTypes(loanId);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getProducerBasicDatas] error: ",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return result;
	}
	
	
	/**
	 * 获取进件子系统基表数据-客户信息tab
	 * @return
	 */
	@RequestMapping("/getCustomerBasicDatas")
	@ResponseBody
	public MarvelsResultDTO getCustomerBasicDatas(){
		MarvelsResultDTO result = null;
		try {
			result = taskProcessService.getCustomerBasicDatas();
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getCustomerBasicDatas] error: ",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return result;
	}
	
	/**
	 * 获取贷款基础信息
	 * @param loanId
	 * @return
	 */
	@RequestMapping("/getLoanInfo/{loanId}")
	@ResponseBody
	public MarvelsResultDTO getLoanInfo(@PathVariable("loanId") String loanId){
		McamLoanApplyInfoDTO result = null;
		try {
			result = taskProcessService.getLoanInfo(loanId);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getLoanInfo] loanId = "+loanId,e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), result);
	}
	
	/**
	 * 获取贷款信息详情
	 * @param loanId
	 * @return
	 */
	@RequestMapping("/getLoanDetail/{loanId}")
	@ResponseBody
	public MarvelsResultDTO getLoanDetail(@PathVariable("loanId") String loanId){
		McamLoanApplyInfoDTO result = null;
		try {
			result = taskProcessService.getLoanDetail(loanId);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getLoanDetail] loanId = "+loanId,e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), result);
	}
	
	/**
	 * 保存贷款信息
	 * @param loanInfo
	 * @return
	 */
	@RequestMapping("/saveLoanInfo")
	@ResponseBody
	public MarvelsResultDTO saveLoanInfo(@RequestBody McamLoanApplyInfoDTO loanInfo){
		McamLoanApplyInfoDTO result = null;
		try {
			result = taskProcessService.saveLoanInfo(loanInfo);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[saveLoanInfo] loanInfo = "+loanInfo.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), result);
	}
	
	/**
	 * 删除房产信息
	 * @param estateInfo
	 * @return
	 */
	@RequestMapping("/deleteEstateInfo")
	@ResponseBody
	public MarvelsResultDTO deleteEstateInfo(@RequestBody EstateInfoDTO estateInfo){
		Integer result = null;
		try {
			result = taskProcessService.deleteEstateInfo(estateInfo);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[deleteEstateInfo] estateInfo = "+estateInfo.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), result);
	}
	
	/**
	 * 获取客户信息详情
	 * @param loanId
	 * @return
	 */
	@RequestMapping("/getCustomerDetail/{loanId}")
	@ResponseBody
	public MarvelsResultDTO getCustomerDetail(@PathVariable("loanId") String loanId){
		McamLoanApplyInfoDTO result = null;
		try {
			result = taskProcessService.getCustomerDetail(loanId);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getCustomerDetail] loanId = "+loanId,e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), result);
	}
	
	/**
	 * 保存客户信息
	 * @param loanInfo
	 * @return
	 */
	@RequestMapping("/saveCustomerInfo")
	@ResponseBody
	public MarvelsResultDTO saveCustomerInfo(@RequestBody McamLoanApplyInfoDTO loanInfo){
		McamLoanApplyInfoDTO result = null;
		try {
			result = taskProcessService.saveCustomerInfo(loanInfo);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[saveCustomerInfo] loanInfo = "+loanInfo.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), result);
	}
	
	/**
	 * 查询登录用户信息
	 * @return
	 */
	@RequestMapping("/getLoginUserInfo")
	@ResponseBody
	public MarvelsResultDTO getLoginUserInfo(){
		SysUserDTO user = null;
		try {
			user = taskProcessService.getLoginUserInfo();
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getLoginUserInfo] error ",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), user);
	}
	
	/**
	 * 电核记录-拨号回调          暂未通
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/writeCallInfo/{id}")
	@ResponseBody
	public MarvelsResultDTO writeCallInfo(@PathVariable("id") Long id,HttpServletRequest request){
		SysUserDTO user = null;
		try {
			user = taskProcessService.getLoginUserInfo();
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getLoginUserInfo] error ",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), user);
	}
	
	/**
	 * 历史贷款记录分页查询
	 * @param loanInfo
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/getHistoryLoanInfoPage/{pageNo}/{pageSize}")
	@ResponseBody
	public MarvelsResultDTO getHistoryLoanInfoPage(@RequestBody McamLoanApplyInfoDTO loanInfo,@PathVariable("pageNo") Integer pageNo,@PathVariable("pageSize") Integer pageSize){
		Page<McamLoanApplyInfoDTO> pageList = null;
		try {
			pageList = taskProcessService.getHistoryLoanInfoPage(loanInfo, pageNo, pageSize);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getHistoryLoanInfoPage] error ",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), pageList);
	}
	
	/**
	 * 欺诈检核结果分页查询
	 * @param loanInfo
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/geTrickCheckInfoPage/{pageNo}/{pageSize}")
	@ResponseBody
	public MarvelsResultDTO geTrickCheckInfoPage(@RequestBody McamLoanApplyInfoDTO loanInfo,@PathVariable("pageNo") Integer pageNo,@PathVariable("pageSize") Integer pageSize){
		Page<TrickCheckInfoDTO> pageList = null;
		try {
			pageList = taskProcessService.getTrickCheckInfoPage(loanInfo, pageNo, pageSize);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[geTrickCheckInfoPage] error ",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), pageList);
	}
	
	/**
	 * 风险检核结果分页查询
	 * @param loanInfo
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/getRiskCheckInfoPage/{pageNo}/{pageSize}")
	@ResponseBody
	public MarvelsResultDTO getRiskCheckInfoPage(@RequestBody McamLoanApplyInfoDTO loanInfo,@PathVariable("pageNo") Integer pageNo,@PathVariable("pageSize") Integer pageSize){
		Page<RiskCheckInfoDTO> pageList = null;
		try {
			pageList = taskProcessService.getRiskCheckInfoPage(loanInfo, pageNo, pageSize);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getRiskCheckInfoPage] error ",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), pageList);
	}
	
	/**
	 * 刷新欺诈和风险检核结果数据
	 * @param loanId
	 * @return
	 */
	@RequestMapping("/updateTrickAndRiskCheckData/{loanId}")
	@ResponseBody
	public MarvelsResultDTO updateTrickAndRiskCheckData(@PathVariable("loanId") String loanId){
		try {
			taskProcessService.updateTrickAndRiskCheckData(loanId);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[updateTrickAndRiskCheckData] error ",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null);
	}
	
	/**
	 * 获取流转记录
	 * @param loanId
	 * @return
	 */
	@RequestMapping("/getLoanApplyCaseLogs/{loanId}")
	@ResponseBody
	public MarvelsResultDTO getLoanApplyCaseLogs(@PathVariable("loanId") String loanId){
		LoanApplyCaseDTO loanApplyCase = null;
		try {
			loanApplyCase = taskProcessService.getLoanApplyCaseLogs(loanId);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getLoanApplyCaseLogs] error ",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), loanApplyCase);
	}
	
	@RequestMapping("/getReturnReasonList")
	@ResponseBody
	public MarvelsResultDTO getReturnReasonList(){
		List<OpinionTypeDTO> data = null;
		try {
			data = taskProcessService.getReturnReasonList();
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e);
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getReturnReasonList] error ",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), data);
	}
}
